package cn.king.springframework.beans.factory.support;

import cn.king.springframework.beans.BeanException;
import cn.king.springframework.beans.factory.BeanFactory;
import cn.king.springframework.beans.factory.config.BeanDefinition;

public abstract  class AbstractBeanFactory  extends DefaultSingletonBeanRegistry implements BeanFactory{
    @Override
    public Object getBean(String name) throws BeanException {
        return doGetBean(name,null);
    }

    @Override
    public Object getBean(String name, Object... args) {

        return doGetBean(name,args);
    }


    protected  <T> T doGetBean(final String name,final Object[] args){
        Object bean = getSingleton(name);
        if(bean != null){
            return (T)bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return (T)createBean(name,beanDefinition,args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requireType) {

        return (T)getBean(name);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeanException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeanException;
}
